package App.Controller;

import App.Model.BaseResponse;
import App.Model.Chat.ChatGroupListRequest;
import App.Model.Friend.FriendListRequest;
import App.Model.Friend.FriendListResponse;
import App.Model.LoginResponse;
import App.Model.Project.ProjectGetListRequest;
import App.Model.Project.ProjectList;
import App.Model.Task.TaskInfoList;
import App.Model.Task.TaskListRequest;
import App.Model.Utility.*;
import App.Repository.ChatRepository;
import App.Service.FriendService;
import App.Service.ProjectService;
import App.Service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import App.Model.Chat.ChatGroupListPageResponse;

import java.util.List;

/**
 * Created by xw on 2019/9/26.
 */

@RestController
@RequestMapping("/api/utility/")
public class UtilityController extends BaseController {

    @Autowired
    private ChatRepository _chatRepository;

    @Autowired
    private ProjectService _projectService;

    @Autowired
    private TaskService _taskService;

    @Autowired
    private FriendService _friendService;

    //全局搜索
    @RequestMapping(value = "search", method = RequestMethod.POST)
    public ResponseEntity<LoginResponse> search(@RequestBody(required = false) GlobalSearchRequest _request) {
        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        GlobalSearchResponse _response=new GlobalSearchResponse();
        try {
            String _searchKey = _request.getSearchkey();

            ChatGroupListRequest _chatRequest = new ChatGroupListRequest();
            _chatRequest.setSearchkey(_searchKey);
            _chatRequest.setPageindex(_request.getPageindex());
            _chatRequest.setPagesize(_request.getPagesize());
            ChatGroupListPageResponse chats = _chatRepository.getGroupList(userId(),_chatRequest);
            _response.setChats(chats.getDataList());

            ProjectGetListRequest _projectRequest=new ProjectGetListRequest();
            _projectRequest.setSearchkey(_searchKey);
            _projectRequest.setPageindex(_request.getPageindex());
            _projectRequest.setPagesize(_request.getPagesize());
            List<ProjectList> _projects = _projectService.getProjectlist(_projectRequest, userId(), "project");
            _response.setProjects(_projects);

            TaskListRequest _taskRequest=new TaskListRequest();
            _taskRequest.setSearchkey(_searchKey);
            _taskRequest.setPageindex(_request.getPageindex());
            _taskRequest.setPagesize(_request.getPagesize());
            List<TaskInfoList> _tasks = _taskService.getTaskList(_taskRequest, userId());
            _response.setTasks(_tasks);

            FriendListRequest _frRequest = new FriendListRequest();
            _frRequest.setSearchkey(_searchKey);
            _frRequest.setPageindex(_request.getPageindex());
            _frRequest.setPagesize(_request.getPagesize());
            List<FriendListResponse>  _friend = _friendService.getFriendList(_frRequest, userId());
            _response.setContacts(_friend);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_response, HttpStatus.OK);
    }

    //全局搜索
    @RequestMapping(value = "searchadv", method = RequestMethod.POST)
    public ResponseEntity<LoginResponse> searchAdvance(@RequestBody(required = false) GlobalSearchRequest _request) {
        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        GlobalSearchResponse _response=new GlobalSearchResponse();
        try {
            String _searchKey = _request.getSearchkey();

            ChatGroupListRequest _chatRequest = new ChatGroupListRequest();
            _chatRequest.setSearchkey(_searchKey);
            _chatRequest.setPageindex(_request.getPageindex());
            _chatRequest.setPagesize(_request.getPagesize());
            ChatGroupListPageResponse chats = _chatRepository.getGroupList(userId(),_chatRequest);
            _response.setChats(chats);

            ProjectGetListRequest _projectRequest=new ProjectGetListRequest();
            _projectRequest.setSearchkey(_searchKey);
            _projectRequest.setPageindex(_request.getPageindex());
            _projectRequest.setPagesize(_request.getPagesize());
            List<ProjectList> _projects = _projectService.getProjectlist(_projectRequest, userId(), "project");
            _response.setProjects(_projects);

            TaskListRequest _taskRequest=new TaskListRequest();
            _taskRequest.setSearchkey(_searchKey);
            _taskRequest.setPageindex(_request.getPageindex());
            _taskRequest.setPagesize(_request.getPagesize());
            List<TaskInfoList> _tasks = _taskService.getTaskList(_taskRequest, userId());
            _response.setTasks(_tasks);

            FriendListRequest _frRequest = new FriendListRequest();
            _frRequest.setSearchkey(_searchKey);
            _frRequest.setPageindex(_request.getPageindex());
            _frRequest.setPagesize(_request.getPagesize());
            List<FriendListResponse>  _friend = _friendService.getFriendSearchList(_frRequest, userId());
            _response.setContacts(_friend);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_response, HttpStatus.OK);
    }

    @RequestMapping(value = "searchlist", method = RequestMethod.POST)
    public ResponseEntity<LoginResponse> searchList(@RequestBody(required = false) GlobalSearchDetailListRequest _request) {
        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        GlobalSearchDetailList _response = new GlobalSearchDetailList();
        try {
            List<GlobalSearchDetailListResponse> res = _chatRepository.searchDetailListMessage(_request, userId());
            _response.setData(res);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_response, HttpStatus.OK);
    }

    @RequestMapping(value = "searchdetail", method = RequestMethod.POST)
    public ResponseEntity<LoginResponse> searchDetail(@RequestBody(required = false) GlobalSearchDetailRequest _request) {
        if (_request == null) {
            BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        GlobalSearchDetail _response = new GlobalSearchDetail();

        try {
            List<GlobalSearchDetailResponse> res = _chatRepository.searchDetailMessage(_request, userId());
            _response.setData(res);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_response, HttpStatus.OK);
    }
}
